package com.example.gamestore.control;

import com.example.gamestore.Bean.EmailRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@Controller
public class EmailControl {
    @Autowired
    private JavaMailSender javaMailSender;
    //这一步是获取application.properties中设置的发件人邮箱地址
    @Value("${spring.mail.username}")
    private String username;
    @PostMapping("/sendemail")
    @ResponseBody
    public String sendemail(HttpServletRequest request){
        EmailRequest emailRequest = (EmailRequest) request.getAttribute("wjs");
        String email = emailRequest.getEmail();
        String username = emailRequest.getUsername();
        String password = emailRequest.getPassword();
        SimpleMailMessage message = new SimpleMailMessage();
        //发件人邮件地址(上面获取到的，也可以直接填写,string类型)
        message.setFrom(this.username);
        //要发送的qq邮箱(收件人地址)
        message.setTo(email);//address
        //邮件主题
        message.setSubject("人家想要一个管理员账号呢");
        //邮件正文
        message.setText("用户名："+username+"密码："+password);//！！！
        javaMailSender.send(message);
        return "发送成功";
    }

   @PostMapping("/emailreq")
    public String emailreq(Model model, EmailRequest emailRequest){
       System.out.println(model);
       System.out.println(emailRequest);
        model.addAttribute("wjs",emailRequest);
        return "forward:/sendemail";
    }
}
